package kr.hkit.summury_java.erp.serviceUI;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summury_java.erp.dao.DaoInterface;
import kr.hkit.summury_java.erp.dto.CodeTitle;

public class PanelCodeTitleInputUI extends JPanel implements ActionListener {
	private JPanel panel_top;
	private JPanel panel_bottom;
	private JPanel pAlarm;
	private InputPanel pTitleNo;
	private InputPanel pTitleName;
	private JButton btn;
	private JLabel lblAlarm;
	
	private DaoInterface<CodeTitle> dao;
	/**
	 * Create the panel.
	 */
	public PanelCodeTitleInputUI() {

		initialize();
	}
	
	public PanelCodeTitleInputUI(String btnName,
								DaoInterface<CodeTitle> dao) {
		initialize();
		this.btn.setText(btnName);
		this.dao = dao;
	}
	
	public JPanel getPanel_top() {
		return panel_top;
	}

	public void setPanel_top(JPanel panel_top) {
		this.panel_top = panel_top;
	}

	public JPanel getPanel_bottom() {
		return panel_bottom;
	}

	public void setPanel_bottom(JPanel panel_bottom) {
		this.panel_bottom = panel_bottom;
	}

	public JPanel getpAlarm() {
		return pAlarm;
	}

	public void setpAlarm(JPanel pAlarm) {
		this.pAlarm = pAlarm;
	}

	public InputPanel getpTitleNo() {
		return pTitleNo;
	}

	public void setpTitleNo(InputPanel pTitleNo) {
		this.pTitleNo = pTitleNo;
	}

	public InputPanel getpTitleName() {
		return pTitleName;
	}

	public void setpTitleName(InputPanel pTitleName) {
		this.pTitleName = pTitleName;
	}

	public JButton getBtn() {
		return btn;
	}

	public void setBtnN(JButton btn) {
		this.btn = btn;
	}

	public JLabel getLblAlarm() {
		return lblAlarm;
	}

	public void setLblAlarm(JLabel lblAlarm) {
		this.lblAlarm = lblAlarm;
	}

	public DaoInterface<CodeTitle> getDao() {
		return dao;
	}
	
	public void setDao(DaoInterface<CodeTitle> dao) {
		this.dao = dao;
	}	
	
	private void initialize() {
		setLayout(new BorderLayout(0, 30));
		
		panel_top = new JPanel();
		add(panel_top, BorderLayout.CENTER);
		panel_top.setLayout(new GridLayout(0, 1, 0, 40));
		
		pAlarm = new JPanel();
		panel_top.add(pAlarm);
		
		lblAlarm = new JLabel("New label");
		lblAlarm.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		pAlarm.add(lblAlarm);
		
		pTitleNo = new InputPanel("직급번호");
		panel_top.add(pTitleNo);
		
		pTitleName = new InputPanel("직급명");
		panel_top.add(pTitleName);
		
		panel_bottom = new JPanel();
		add(panel_bottom, BorderLayout.SOUTH);
		
		btn = new JButton("New button");
		btn.addActionListener(this);
		btn.setBackground(SystemColor.inactiveCaption);
		btn.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		panel_bottom.add(btn);
	}
	
	private boolean inValidCheck() {
		//공백만 체크
			if(pTitleNo.getTf().getText().equals("")) {
				pTitleNo.getTf().requestFocus();
				return false;
			}
			if(pTitleName.getTf().getText().equals("")) {
				pTitleName.getTf().requestFocus();
				return false;
			}		
		return true;
	}
	
	private CodeTitle getInfo() {
		if (inValidCheck()){
			int titleNo = Integer.parseInt(pTitleNo.getTf().getText().trim());
			String name = pTitleName.getTf().getText();		
			return new CodeTitle(titleNo, name);
		}
			return null; 
		
	}
	
	private void setIniTf() { //텍스트필드 초기화
		pTitleNo.getTf().setText("");
		pTitleName.getTf().setText("");
	}
	
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn) {
			actionPerformedBtn(e);
		}
	}
	protected void actionPerformedBtn(ActionEvent e) {
		try {	
			if(btn.getText().equals("추가")) {
				CodeTitle c = getInfo();
				dao.insertItem(c);
				JOptionPane.showMessageDialog(this, "정상적으로 입력되었습니다.");
				setIniTf();
			}
			
			if(btn.getText().equals("수정")) {
				CodeTitle c = getInfo();
				dao.updateItemByNo(c);
				JOptionPane.showMessageDialog(this, "정상적으로 수정되었습니다.");
				setIniTf();
				
			}
			
			if(btn.getText().equals("삭제")) {
				int titleNo = Integer.parseInt(pTitleNo.getTf().getText().trim());
				CodeTitle c = new CodeTitle(titleNo);
				dao.deleteItemByNo(c);
				JOptionPane.showMessageDialog(this, "정상적으로 삭제되었습니다.");
				setIniTf();
			}
			
			if(btn.getText().equals("검색")) {
				int titleNo = Integer.parseInt(pTitleNo.getTf().getText().trim());
				CodeTitle c = new CodeTitle(titleNo);
				JOptionPane.showMessageDialog(this, dao.searchItem(c));
				setIniTf();
			}
			
		} catch (SQLException e1) {
			e1.printStackTrace();
//			JOptionPane.showMessageDialog(this, "입력을 실패하였습니다. 다시 확인하여 주세요.");
		} catch (NumberFormatException e2) {
			JOptionPane.showMessageDialog(this, "정보를 확인하여 입력해주세요.");
		}
	}
}
